package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Box {
                        BirthdayGreetingWithImage(message = "happy birthday", from = "jiahe")
                        Column {
                            Greeting("Android")
                            BirthdayCard("jiahe;")
                            btnClick()
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(
        text = "Hello $name!", modifier = Modifier
            .padding(15.dp)
            .background(Color.Green)
            .fillMaxWidth()
            .wrapContentWidth(align = Alignment.CenterHorizontally)
            .padding(5.dp),
        fontSize = 25.sp
    )
}

@Composable
fun BirthdayCard(name: String) {
    Text(
        text = "Happy birthday -- $name!",
        modifier = Modifier
            .background(
                color = Color(R.color.purple_200),
                shape = RoundedCornerShape(4.dp)
            )
            .fillMaxWidth()
            .wrapContentWidth(align = Alignment.CenterHorizontally)
            .padding(15.dp),
        fontSize = 20.sp,
        color = Color.White
    )
}

@Composable
fun BirthdayGreetingWithImage(message: String, from: String) {
    val image = painterResource(id = R.drawable.qr_code)
    Image(
        painter = image,
        contentDescription = "QR Code",
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize(),
        contentScale = ContentScale.Crop
    )
}

@Composable
fun btnClick() {
    val context = LocalContext.current
    Button(onClick = {
        val intent = Intent(context, CalculatorActivity::class.java)
        context.startActivity(intent)
    }) {
        Text(text = "跳转")
    }
}

@Preview(showBackground = true)
@Composable
fun BirthdayCardPreview() {
    MyApplicationTheme {
        Box {
            BirthdayGreetingWithImage(message = "happy birthday", from = "jiahe")
            Column {
                Greeting("Android")
                BirthdayCard("jiahe")
                btnClick()
            }
        }
    }
}